<template>
  <div class="home">
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <div>
          <img src="../assets/logo.png" alt="" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!-- 内容区域 -->
      <el-container>
        <!-- 左侧列表区域 -->
        <el-aside :width="toggieFlag ? '64px' : '200px'">
          <!-- 侧边栏菜单区域 -->
          <el-menu
            router
            :collapse="toggieFlag"
            unique-opened
            :collapse-transition="false"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409Eff"
            :default-active="$route.path"
          >
            <div class="toggle-button" @click="toggieCollapse">|||</div>
            <!-- 一级菜单 -->
            <el-submenu
              :index="item.id + ''"
              v-for="item in lists"
              :key="item.id"
            >
              <!-- 一级菜单模板 -->
              <template slot="title">
                <i :class="icon[item.id]" class="icon"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/' + it.path"
                v-for="it in item.children"
                :key="it.id"
                @click="higHlight(item.authName, it.authName)"
              >
                <!-- 二级模板区域 -->
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ it.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧内容区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "Home",
  components: {},
  data() {
    return {
      // 左侧菜单数据
      lists: [],
      icon: {
        125: "iconfont icon-users",
        103: "el-icon-reading",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      // 左侧菜单开关
      toggieFlag: false,
      // 高亮显示
      activePath: "",
    };
  },
  mounted() {
    this.getList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    logout() {
      window.sessionStorage.removeItem("token");
      window.sessionStorage.removeItem("activePath");
      this.$router.push("/login");
    },
    async getList() {
      const { data: res } = await this.$http.get("menus");
      // console.log(res);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.lists = res.data;
      // console.log(this.lists);
    },
    // 左侧菜单开关
    toggieCollapse() {
      this.toggieFlag = !this.toggieFlag;
    },
    // 面包屑导航
    higHlight(one, two) {
      // window.sessionStorage.setItem("activePath", activePath);
      // this.activePath = activePath;
      const obj = {
        one: one,
        two: two,
      };
      window.sessionStorage.setItem("name", JSON.stringify(obj));
    },
  },
  // watch: {
  //   // 浏览器页面回退高亮显示
  //   $route(to, from) {
  //     this.activePath = to.path;
  //     window.sessionStorage.setItem("activePath", to.path);
  //   },
  // },
};
</script>
<style lang="scss" scoped>
.home {
  height: 100%;
}
.el-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #373d41;
  color: #333;
  // padding-left: 0;
  div {
    display: flex;
    align-items: center;
    img {
      width: 60px;
      height: 60px;
      border-radius: 50%;
      background-color: #eaedea;
    }
    span {
      color: #fff;
      font-size: 20px;
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  color: #333;
}

.el-main {
  background-color: #eaedea;
  color: #333;
}

.el-menu {
  border-right: 0;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
}
.icon {
  margin-right: 15px;
}
</style>